#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'moxuan'

import urllib2
import re
import time


# 糗事百科
class QSBK:
    def __init__(self):
        self.pageIndex = 1
        self.user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) ' \
                          'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'
        self.headers = {'User-Agent': self.user_agent}
        self.stories = []
        self.enable = False

    # 根据页码获得当前页面的内容
    def getPage(self, pageIndex):
        try:
            url = 'http://www.qiushibaike.com/hot/page/' + str(pageIndex)
            request = urllib2.Request(url, headers=self.headers)
            response = urllib2.urlopen(request)
            content = response.read()
            return content
        except urllib2.URLError, e:
            if hasattr(e, 'code'):
                print(e.code)
            if hasattr(e, 'reason'):
                print(e.reason)
            return None

    # 根据页码提取当前页面段子的列表
    def getPageItems(self, pageIndex):
        content = self.getPage(pageIndex)
        if not content:
            print '没有数据'
            return None
        # 发布人,段子内容, 发布日期,文字/图片/视频 , 点赞个数
        pattern = re.compile('<div.*?author">.*?<a.*?<img.*?>(.*?)</a>.*?' +
                             '<div.*?content">(.*?)<!--(.*?)-->.*?</div>(.*?)' +
                             '<div class="stats.*?class="number">(.*?)</i>', re.S)
        items = re.findall(pattern, content)
        pageStories = []
        for item in items:
            havImg = re.search('img', item[3])
            havVideo = re.search('video', item[3])
            if not havImg and not havVideo:
                replaceBR = re.compile('<br/>')
                text = re.sub(replaceBR, "\n", item[1])
                pageStories.append([item[0].strip(), text.strip(), item[2].strip(), item[4].strip()])
        return pageStories

    # 加载并提取页面的内容, 加载到列表中
    def loadPage(self):
        if self.enable == True:
            # 如果当前未看的故事少于, 加载下一页的数据
            if len(self.stories) < 2:
                pageStories = self.getPageItems(self.pageIndex)
                if pageStories:
                    self.stories.append(pageStories)
                    self.pageIndex += 1

    # 敲一次回车打印一个段子
    def getOneStory(self, pageStories, page):
        for story in pageStories:
            input = raw_input()
            self.loadPage()
            if input == 'Q':
                self.enable = False
                return
            print '第%d页\t发布人:%s\t发布时间:%s\t赞:%s\n%s' % (page, story[0], story[2], story[3], story[1])

    def start(self):
        print u'正在读取, 按回车查看, Q退出'
        self.enable = True
        self.loadPage()
        nowPage = 0
        while self.enable:
            if len(self.stories) > 0:
                pageStories = self.stories[0]
                nowPage += 1
                del self.stories[0]
                self.getOneStory(pageStories, nowPage)


if __name__ == "__main__":
    spider = QSBK()
    spider.start()
